package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.entity.Cate;
import com.github.pagehelper.Page;
import com.service.CateService;
import com.util.VeDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController //定义为控制器 返回JSON类型数据
@RequestMapping(value = "/cate", produces = "application/json; charset=utf-8")// 设置请求路径
@CrossOrigin // 允许跨域访问其资源
public class CateController { // extends BaseController {
    // TODO Auto-generated method stub

    @Autowired // @Autowired的作用是自动注入依赖的ServiceBean
    private CateService cateService;

    // 预处理 获取基础参数
    @GetMapping(value = "createCate.action")
    public Map<String, Object> createCate() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("today", VeDate.getStringDateShort());
        return map;
    }

    // 新增商品类型
    @PostMapping(value = "insertCate.action")
    public Map<String, Object> insertCate(@RequestBody String jsonStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
        Cate cate = new Cate();
        cate.setCatename(obj.getString("catename")); //  为类型名称赋值
        cate.setAddtime(VeDate.getStringDateShort()); // 为创建日期赋值
        cate.setMemo(obj.getString("memo")); //  为备注赋值
        int num = this.cateService.insertCate(cate);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "保存成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "保存失败");
        }
        return map;
    }

    // 按主键删除一个商品类型
    @GetMapping(value = "deleteCate.action")
    public Map<String, Object> deleteCate(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        int num = this.cateService.deleteCate(id);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "删除失败");
        }
        return map;
    }

    // 按主键批量删除商品类型
    @PostMapping(value = "deleteCateByIds.action")
    public Map<String, Object> deleteCateByIds(@RequestBody String[] ids) {
        int num = 0;
        for (String cateid : ids) {
            num += this.cateService.deleteCate(cateid);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "删除失败");
        }
        return map;
    }

    // 修改商品类型
    @PostMapping(value = "updateCate.action")
    public Map<String, Object> updateCate(@RequestBody String jsonStr) {
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
        Cate cate = this.cateService.getCateById(obj.getString("cateid")); // 获取object中cateid字段
        cate.setCatename(obj.getString("catename")); //  为类型名称赋值
        cate.setMemo(obj.getString("memo")); //  为备注赋值

        Map<String, Object> map = new HashMap<String, Object>();
        int num = this.cateService.updateCate(cate);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "修改成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "修改失败");
        }
        return map;
    }

    // 查询全部商品类型数据 在下拉菜单中显示
    @GetMapping(value = "getAllCate.action")
    public List<Cate> getAllCate() {
        return this.cateService.getAllCate();
    }

    // 按关键字查询商品类型数据 在下拉菜单中显示
    @GetMapping(value = "getCateMap.action")
    public Map<String, Object> getCateMap(String keywords) {
        Map<String, Object> map = new HashMap<String, Object>();
        Cate cate = new Cate();
        cate.setCatename(keywords);
        List<Cate> list = this.cateService.getCateByLike(cate);
        map.put("data", list);
        return map;
    }

    // 通过AJAX在表格中显示商品类型数据
    @GetMapping(value = "getCateByPage.action")
    public Map<String, Object> getCateByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
        // 定义一个Map对象 用来返回数据
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Cate> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
        List<Cate> list = this.cateService.getAllCate();
        // 返回的map中定义数据格式
        map.put("count", pager.getTotal());
        map.put("total", list.size());
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("page", page);
        map.put("limit", limit);
        return map;
    }

    // 通过AJAX在表格中显示商品类型数据
    @GetMapping(value = "getCate.action")
    public Map<String, Object> getCate(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String keywords) {
        // 定义一个Map对象 用来返回数据
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Cate> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
        Cate cate = new Cate();
        cate.setCatename(keywords);
        List<Cate> list = this.cateService.getCateByLike(cate);
        // 返回的map中定义数据格式
        map.put("count", pager.getTotal());
        map.put("total", list.size());
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("page", page);
        map.put("limit", limit);
        return map;
    }

    // 通过AJAX在表格中显示商品类型数据
    @GetMapping(value = "getOwnerCate.action")
    public Map<String, Object> getOwnerCate(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer limit, String id) {
        // 定义一个Map对象 用来返回数据
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Cate> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
        Cate cate = new Cate();
        //cate.setAdminid(id);
        List<Cate> list = this.cateService.getCateByLike(cate);
        // 返回的map中定义数据格式
        map.put("count", pager.getTotal());
        map.put("total", list.size());
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("page", page);
        map.put("limit", limit);
        return map;
    }

    // 按主键查询商品类型数据
    @GetMapping(value = "getCateById.action")
    public Cate getCateById(String id) {
        Cate cate = this.cateService.getCateById(id);
        return cate;
    }

    // TODO Auto-generated method stub
}
//作者联系 QQ 1420527913   毕设成品下载网址  codebag.cn


